package com.yami.shop.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.MeltCloudGroupUser;
import com.yami.shop.dao.MeltCloudGroupUserMapper;
import com.yami.shop.service.MeltCloudGroupUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class MeltCloudGroupUserServiceImpl extends ServiceImpl<MeltCloudGroupUserMapper, MeltCloudGroupUser> implements MeltCloudGroupUserService {


    /**
     * 新增用户群组
     *
     * @param user
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertGroupUser(MeltCloudGroupUser user) {
        this.save(user);
    }

    /**
     * 批量新建群组
     *
     * @param user
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertBatchGroupUsers(List<MeltCloudGroupUser> user) {
        this.saveBatch(user);
    }

    /**
     * 禁言群成员
     *
     * @param groupId
     * @param groupUser
     */
    @Override
    public void taboo(String groupId, List<String> groupUser) {
        LambdaQueryWrapper<MeltCloudGroupUser> wrapper = new LambdaQueryWrapper<MeltCloudGroupUser>()
                .eq(MeltCloudGroupUser::getGroupId, groupId)
                .in(MeltCloudGroupUser::getUserId, groupUser);
        List<MeltCloudGroupUser> list = this.list(wrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            list.forEach(user -> {
                user.setUserStatus("1");
            });
            this.updateBatchById(list);
        }
    }

    /**
     * 取消禁言群成员
     *
     * @param groupId
     * @param groupUser
     */
    @Override
    public void cancelTaboo(String groupId, List<String> groupUser) {
        LambdaQueryWrapper<MeltCloudGroupUser> wrapper = new LambdaQueryWrapper<MeltCloudGroupUser>()
                .eq(MeltCloudGroupUser::getGroupId, groupId)
                .in(MeltCloudGroupUser::getUserId, groupUser);
        List<MeltCloudGroupUser> list = this.list(wrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            list.forEach(user -> {
                user.setUserStatus("0");
            });
            this.updateBatchById(list);
        }
    }
}
